<template>
	<view class="container">
		<header-bar :config='config'></header-bar>
		<view class="main">
			<view class="card-cell type-cell" v-if="state==3">
				<view class="cell-title flex-s-c" style="width: 100%;">
					<view>送达备注:</view>
				</view>
				<textarea class="textarea" :maxlength="maxLengthText" @input="sum" v-model="content"
					placeholder="请输入备注内容"></textarea>
				<view class="number"><text class="pr10"><text class="strong">{{num}}</text>/{{maxLengthText}} </text>
				</view>
			</view>
			<view class="card-cell type-cell img-type-cell" v-if="state==3">
				<text class="cell-title">上传图片(必填)</text>
				<view class="img-cell">
					<view class="" v-if="imageList.length < 9">
						<view class="add-img" @tap="chooseImage">
							<uni-icons type="camera" size="50"></uni-icons>
							<text>添加图片</text>
						</view>
					</view>
					<block v-for="(image,index) in imageList" :key="index">
						<view class="img-item">
							<image class="img" :src="image" :data-src="image" @tap="previewImage"></image>
							<view @click="del(index)">
								<uni-icons class="btn-del" type="closeempty" customIcons="icon-white"></uni-icons>
							</view>

						</view>
					</block>
				</view>
			</view>
			<view class="card-cell type-cell" v-if="state==4">
				<view class="cell-title flex-s-c" style="width: 100%;">
					<view>送达备注:</view>
				</view>
				<textarea class="textarea" disabled :maxlength="maxLengthText" @input="sum"
					:value="data.completeNotice||'暂无'"
					placeholder=""></textarea>

			</view>
			<view class="card-cell type-cell img-type-cell" v-if="state==4">
				<text class="cell-title">上传图片</text>
				<view class="img-cell">
					<block v-for="(image,index) in getImageList" :key="index">
						<view class="img-item">
							<image class="img" :src="image" :data-src="image" @tap="previewGetImage"></image>
						</view>
					</block>
				</view>
			</view>

			<detail-task-item :data="data" :state="state" @showTel="openPopup" @submit="submit"></detail-task-item>
			<view class="item-card">
				<view class="item-card-header bottom-line">
					<text>商品清单</text>
				</view>
				<view class="pro-table">
					<view class="table-header">
						<view class="tr">
							<text class="td pro-name">商品名称</text>
							<text class="td pro-num">数量</text>
							<text class="td pro-weight">重量</text>
						</view>
					</view>
					<view class="table-body">
						<view class="tr" v-for="(item,index) in data.details" :key="index" v-show="index<=4||showMore==true">
							<view class="td pro-name flex-v-c">
							<image class="pro-image" :src="easyGetImgUrl(item.picture)" mode="aspectFill"></image>
							<view>
								<view>{{item.productName}}</view>
								<text style="color:#999">{{item.attribute}}</text class="color-gray-3">
							</view>
							</view>
							<text class="td pro-num">×{{item.amount}}</text>
							<text class="td pro-weight">{{item.weight||0}}kg</text>
						</view>
						<view class="flex-center" v-if="data.details&&data.details.length>5">
							<text class="showmore-btn" :class="showMore==true?'fold-btn':'unfold-btn'" @click="showMore=!showMore">
								{{showMore==true?'收起':'展示全部商品'}}
							</text>
						</view>
					</view>
					<view class="table-footer">
						<view class="tr">
							<text class="td pro-name ">合计：共{{data.severalProduct}}种</text>
							<text class="td pro-num">{{data.totalAmount}}件</text>
							<text class="td pro-weight">{{data.totalWeight||0}}kg</text>
						</view>
					</view>
				</view>
			</view>
			<view class="item-card">
				<view class="item-card-header">
					<text>订单备注</text>
				</view>
				<view class="item-card-main item-card-main-reamrk">
					<text class="reamrk-text">{{data.orderNotice||"暂无"}}</text>
				</view>
			</view>

			<view class="item-card">
				<view class="item-card-header bottom-line">
					<text>订单信息</text>
				</view>
				<view class="item-card-main">

					<view class="order-item">
						<view class="label">收货人:</view>
						<view class="value">{{data.storeLinkName||data.storeName}} {{data.storeLinkPhone}}</view>
					</view>
					<view class="order-item">
						<view class="label">下单时间:</view>
						<view class="value">{{data.orderTime}}</view>
					</view>
				<!-- 	<view class="order-item">
						<view class="label">支付方式:</view>
						<view class="value">{{data.payType}}</view>
					</view> -->
					<view class="order-item">
						<view class="label">距离:</view>
						<view class="value">{{data.disance}}km</view>
					</view>
					<view class="order-item">
						<view class="label">重量:</view>
						<view class="value">{{data.totalWeight}}kg</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 弹窗 -->
		<uni-popup ref="centerPopup" :mask-click="true" @maskClick="canclePopup">
			<view class="toast">
				<view class="toast-type">选择联系人</view>
				<view class="toast-msg">
					<view class="toast-tel" @click="makePhoneCall(linkPhone)">
						仓库联系方式：{{linkPhone||"暂无"}}
					</view>
					<view class="toast-tel" @click="makePhoneCall(storeLinkPhone)">
						门店联系方式：{{storeLinkPhone||"暂无"}}
					</view>
				</view>

			</view>
		</uni-popup>
	</view>
</template>

<script>
	import detailTaskItem from "@/pages_tms/components/order/detail-task-item.vue"
	import easyLabelValue from "@/components/easy/easy-label-value.vue"
	import {
		uploadImage
	} from "@/service/api/file.js"
	import {
		OrderDetail,
		OrderHandle,
		OrderDelivery
	} from "@/pages_tms/service/logistics.js"
	import {
		getLocation
	} from "@/uni-sub-base/utils/mapUtil.js"
	export default {
		components: {
			detailTaskItem,
			easyLabelValue
		},
		data() {
			return {
				config: {
					title: '订单详情',
					centerSlot: true,

				},
				num: 0,
				imageList: [],
				getImageList: [],
				content: '',
				maxLengthText: '100',
				data: {},
				state: 0,
				// 联系电话
				storeLinkPhone: 0,
				linkPhone: 0,
				showMore:false
			}
		},
		mounted() {

		},
		onLoad(e) {
			console.log(e)
			if (e && e.id && e.id != 'undefined') {
				this.orderDetail(e.id)
			} else {
				this.easyToast("请检查订单号是否正确")
				setTimeout(() => {
					this.easyGoBack()
				}, 2000)
			}
		},
		onShow() {},
		methods: {
			sum(e) {
				this.num = e.detail.value.length
			},
			//预览图片
			previewImage(e) {
				var current = e.target.dataset.src
				uni.previewImage({
					current: current,
					urls: this.imageList
				})
			},
			//预览图片
			previewGetImage(e) {
				var current = e.target.dataset.src
				uni.previewImage({
					current: current,
					urls: this.getImageList
				})
			},
			//选择图片
			chooseImage() {
				let that = this
				uni.chooseImage({
					count: 9, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['camera'], //仅限拍照
					success: function(res) {
						that.upLoadPicFn(res)
					}
				});
			},
			//删除图片
			del(index) {
				this.imageList.splice(index, 1)
			},
			//上传图片
			upLoadPicFn(files) {
				for (var i = 0; i < files.tempFilePaths.length; i++) {
					let param = {
						filePath: files.tempFilePaths[i],
						formData: {}
					}
					uploadImage(param).then(e => {
						if (e.Data) {
							this.imageList.push(e.Data.FullPath)
							console.log(this.imageList.length)
							if (this.imageList.length > 9) {
								this.imageList.length = 9
							}
						}
					})
				}
			},
			takeOrderFn() {
				if (this.imageList.length < 1) {
					this.easyToast('请上传图片')
					return
				}

			},
			orderDetail(id) {
				OrderDetail(id).then(res => {
					if (res.State == 1) {
						this.data = res.Data || {}
						this.state = Number(this.data.status) || 0
						this.getImageList=[]
						if(!this.data.payType){
							this.data.payType ="已付款"
						}else if(data.payType==1){
							this.data.payType ="货到付款"
						}
						if(this.data.completeUrl){
							let _list =this.data.completeUrl.split(",")
							console.log(_list)
							_list.forEach(item=>{
								if(item){
									this.getImageList.push(item)
								}
							})
						}
					} else {
						this.easyToast(res.Msg)
					}
				})
			},
			openPopup(data) {
				console.log(data)
				this.storeLinkPhone = data.storeLinkPhone
				this.linkPhone = data.linkPhone
				this.setNoScroll()
				this.$refs.centerPopup.open("center")
			},
			canclePopup() {
				this.setScroll()
				this.$refs.centerPopup.close()
			},
			submit() {
				let param = {}
				param.ids = this.data.id
				param.operateType = this.state
				if (this.state == 3) { //完成订单添加送达备注和图片
					param.completeNotice = this.content
					if (this.imageList && this.imageList.length > 0) {
						param.completeUrl = ''
						this.imageList.forEach(item => {
							param.completeUrl += item + ","
						})
					} else {
						this.easyToast("请上传图片")
						return false
					}
				}
				getLocation(res => {
					param.location = res.longitude + ',' + res.latitude
					this.orderHandle(param)
				}, error => {
					console.log(error)
					this.orderHandle(param)
				})
				
				
			},
			orderHandle(param){
				OrderHandle(param).then(res => {
					if (res.State === 1) {
						uni.hideLoading()
						// 删除当前页
						// 删除当前项
						// 更新下一个状态
						let state = this.state+1
						if(state==4){
							this.easyRemoveCurrentGoTo("/pages_tms/packet/order/list?state=2")
						}else{
							this.easyRemoveCurrentGoTo("/pages_tms/packet/order/list?state=" + state)
						}
					} else {
						uni.hideLoading()
						this.easyToast(res.Msg)
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.main {
		padding: $app-page-padding;
	}

	.item-card {
		background: $color-white;
		border-radius: 8rpx;
		margin-top: 20rpx;
		display: flex;
		flex-direction: column;

		.item-card-header {
			padding: $app-page-padding;

			text {
				font-size: $font-size-28;
				font-weight: bold;
			}
		}

		.item-card-main {
			padding: 10rpx $app-page-padding;
		}

		.item-card-main-reamrk {
			background: #F7F7F7;
			margin: 0 $app-page-padding $app-page-padding;
			border-radius: 8rpx;
			padding: $app-page-padding;
		}
	}

	.bottom-line {
		border-bottom: solid 1rpx $border;
	}

	.reamrk-text {
		font-size: $font-size-28;
	}

	.pro-table {
		.table-header {
			background: #F0F3F8;

			.td {
				color: $app-color-text-desc;
			}
		}

		.table-body {
			.td {
				color: $app-color-text-desc;
			}

			.pro-name {
				color: $app-color-text-main;
				// font-weight: bold;
			}
		}

		.table-footer {
			border-top: solid 1rpx $border;

			.td {
				font-weight: bold;
			}
		}

		.tr {
			display: flex;
			flex-direction: row;
			align-items: center;

			.td {
				display: flex;
				padding: $app-page-padding;
				font-size: $font-size-28;
			}
		}

		.pro-name {
			flex: 6;
			
			.pro-image{
				width: 96rpx;
				height: 96rpx;
				border-radius: 8rpx;
				margin-right: 16rpx;
				flex-shrink: 0;
				flex-grow: 0;
			}
		}

		.pro-num {
			flex: 2;
			justify-content: flex-end;
		}

		.pro-weight {
			flex: 2;

			justify-content: flex-end;
		}
	}

	.type-cell {
		display: flex;
		flex-direction: column;
		align-items: flex-start;
		background: #FFFFFF;
		border-radius: 8rpx;
		padding: 34rpx 24rpx 10rpx 24rpx;
		margin-bottom: 0;

		.cell-title {
			display: flex;
			flex: 1;
			font-size: 30rpx;
			color: $color-gray-1;
			margin-bottom: 32rpx;
		}

		.types {
			display: flex;
			flex-direction: row;
			flex-wrap: wrap;
			justify-content: space-between;

			.btn {
				width: 140rpx;
				padding: 0;
				border-radius: 10rpx;
				font-size: 26rpx;
				height: 52rpx;
				border: 1rpx solid #F6F6F6;
				color: $color-gray-1;
				margin-bottom: 20rpx;
				background: #F6F6F6;
			}

			.btnactive {
				background: #FFFFFF;
				color: #FFAE45;
				border-color: #FFAE45;
			}

			&:after {
				content: ' ';
				width: 48%; //这个宽度和子元素宽度一致
				height: 0;
				display: block;
			}
		}
	}

	.textarea {
		display: flex;
		width: 96%;
		padding: 2%;
		min-height: 140rpx;
		font-size: $font-size-24;
		line-height: 40rpx;
		height: 200rpx;
		background: #F6F7F7;
		border-radius: 8rpx;
		// border: 1rpx solid #ec808d;
	}

	.acton-bar {
		margin-top: 70rpx;

		.btn {
			width: 100%;
			height: 80rpx;
		}

	}

	.number {
		padding-top: 10rpx;
		width: 100%;
		display: flex;
		justify-content: flex-end;
		font-size: 24rpx;

		background: #F6F7F7;
		padding-bottom: 10rpx;
		margin-bottom: 10rpx;

		.pr10 {
			padding-right: 10rpx;
			color: #CCCCCC;
		}

		.strong {
			font-size: 26rpx;
		}

	}

	.img-cell {
		display: flex;
		flex: 1;
		flex-direction: row;
		flex-wrap: wrap;

		&:after {
			content: ' ';
			width: 25%; //这个宽度和子元素宽度一致
			height: 0;
			display: block;
		}

		.img-item {
			position: relative;
			width: 130rpx;
			height: 130rpx;
			margin-bottom: 38rpx;
			margin-right: $app-item-padding;

			.img {
				width: 130rpx;
				height: 130rpx;
				border-radius: $border-radius-10;
			}

			.btn-del {
				display: flex;
				justify-content: center;
				align-items: center;
				position: absolute;
				top: -36rpx;
				right: -36rpx;
				width: 50rpx;
				height: 50rpx;
				border-radius: 40rpx;
				background: rgba($color-black, 0.3);
				color: $color-white;
				margin: 15rpx;
			}
		}

		.add-img {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			width: 128rpx;
			height: 128rpx;
			border-radius: $border-radius-10;
			border: dashed 1rpx $color-gray-6;
			margin-right: $app-item-padding;
			margin-bottom: $app-item-padding;

			text {
				margin-top: 0rpx;
			}
		}
	}

	.img-type-cell {
		padding-top: 10rpx;
		margin-bottom: 20rpx;
	}

	.order-item {
		height: 60rpx;
		display: flex;

		.label {
			flex-grow: 0;
			width: 140rpx;
			line-height: 60rpx;
			font-size: 28rpx;
			text-align: justify;
			text-align-last: justify;
			color: $color-gray-3;
		}

		.value {
			padding-left: 20rpx;
			line-height: 60rpx;
			font-size: 28rpx;
		}
	}
	.showmore-btn{
		margin: 24rpx 0;
		font-size: 24rpx;
		padding: 8rpx 56rpx 8rpx 24rpx;
		border: 2rpx solid #B9C0D0;
		border-radius: 28rpx;
		position: relative;
		&:after{
			position: absolute;
			content: '';
			width: 12rpx;
			height: 12rpx;
			border: 2rpx solid;
			border-color: currentColor currentColor transparent transparent;
		}
	}
	.fold-btn:after{
		transform: rotate(-45deg);
		right: 24rpx;
		top: 22rpx;
	}
	.unfold-btn:after{
		transform: rotate(135deg);
		right: 24rpx;
		top: 10rpx;
	}
</style>
